/*
 * @Author: Jim <jimjiang0909@gmail.com>
 * @Create time:   2016-09-01 16:13:37
 * @Last Modified by:   Jim
 * @Last Modified time: 2016-09-12 09:56:43
 * @File Name: Gruntfile.js
 */

module.exports = function(grunt) {

    grunt.initConfig({

        uglify: {
            options: {
                mangle: false,
            },
            target: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['**/*.js', '!**/*.min.js'],
                    dest: 'dist',
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }],
            },

            base: {
                files: [{
                    expand: true,
                    cwd: 'src/base',
                    src: ['**/*.js', '!**/*.min.js'],
                    dest: 'dist/' + grunt.option('site'),
                    rename: function(dest, src) {
                        return dest + '/base/' + src;
                    }
                }],
            },


            site: {
                files: [{
                    expand: true,
                    cwd: 'src/' + grunt.option('site'),
                    src: ['**/*.js', '!**/*.min.js'],
                    dest: 'dist/' + grunt.option('site') + '/' + grunt.option('site'),
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }],
            },
        },

        cssmin: {
            target: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: '**/*.css',
                    dest: 'dist',
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }]
            },

            base: {
                files: [{
                    expand: true,
                    cwd: 'src/base',
                    src: ['**/*.css'],
                    dest: 'dist/' + grunt.option('site'),
                    rename: function(dest, src) {
                        return dest + '/base/' + src;
                    }
                }],
            },

            site: {
                files: [{
                    expand: true,
                    cwd: 'src/' + grunt.option('site'),
                    src: ['**/*.css'],
                    dest: 'dist/' + grunt.option('site') + '/' + grunt.option('site'),
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }],
            },
        },


        //less插件配置
        less: {
            target: {
                files: [{
                    compress: true,
                    expand: true,
                    cwd: 'src',
                    src: ['**/*.less'],
                    dest: 'src',
                    ext: '.css'
                }],
            },
          /*  dev: {
                options: {
                    compress: true,
                    yuicompress:false
                }
            }*/
        },

        imagemin: {
            target: {
                options: {
                    //优化级别1-7，默认是3，效果不明显                        
                    optimizationLevel: 3,
                },
                files: [{
                    expand: true,
                    cwd: "src",
                    src: ['**/*.{png,jpg,gif,ico}'],
                    dest: "dist",
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }]
            },

           base: {
                options: {
                    //优化级别1-7，默认是3，效果不明显                        
                    optimizationLevel: 3,
                },
                files: [{
                    expand: true,
                    cwd: 'src/base',
                    src: ['**/*.{png,jpg,gif,ico}'],
                    dest: 'dist/' + grunt.option('site'),
                    rename: function(dest, src) {
                        return dest + '/base/' + src;
                    }
                }],
            },


           site: {
                options: {
                    //优化级别1-7，默认是3，效果不明显                        
                    optimizationLevel: 3,
                },
                files: [{
                    expand: true,
                    cwd: 'src/' + grunt.option('site'),
                    src: ['**/*.{png,jpg,gif,ico}'],
                    dest: 'dist/' + grunt.option('site') + '/' + grunt.option('site'),
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }],
            },
        },

        copy: {
            target: {
                expand: true,
                cwd: 'src',
                src: ['**/*.tpl', '**/*.min.js'],
                dest: 'dist/',
                //flatten: true,
                //filter: 'isFile',
                rename: function(dest, src) {
                    return dest + '/' + src;
                }
            },

            base: {
                expand: true,
                cwd: 'src/base',
                src: ['**/*.tpl', '**/*.min.js'],
                dest: 'dist/' + grunt.option('site'),
                //flatten: true,
                //filter: 'isFile',
                rename: function(dest, src) {
                    return dest + '/base/' + src;
                }
            },          

            site: {
                expand: true,
                cwd: 'src/' + grunt.option('site'),
                src: ['**/*.tpl', '**/*.min.js'],
                dest: 'dist/' + grunt.option('site') + '/' + grunt.option('site'),
                //flatten: true,
                //filter: 'isFile',
                rename: function(dest, src) {
                        return dest + '/' + src;
                }
            },  
        },

        htmlmin: {
            target: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true,
                    minifyJS: true,
                    minifyCSS: true
                },
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['**/*.html'],
                    dest: 'dist',
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }]
            },

            site: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true,
                    minifyJS: true,
                    minifyCSS: true
                },
                files: [{
                    expand: true,
                    cwd: 'src/' + grunt.option('site'),
                    src: ['**/*.html'],
                    dest: 'dist/' + grunt.option('site') + '/' + grunt.option('site'),
                    rename: function(dest, src) {
                        return dest + '/' + src;
                    }
                }]

            },
        },



        shell: {
            options: {
                stderr: false,
                execOptions: {
                    maxBuffer: 2000 * 1024
                },
            },

            target: {
                command: 'sed -i "" "s/version=\'[0-9.]*\'/version=\'<%=version%>\'/g" `grep \'\'version=.*\'\' -rl ./src`', //Mac
            },

            single: {
                command: 'sed -i "" "s/version=\'[0-9.]*\'/version=\'<%=version%>\'/g" `grep \'\'version=.*\'\' -rl ./src/\'<%=site%>\'`', //Mac
            },

            copy_base: {
                // command: 'for i in `find dist -type d -maxdepth 1 ! -path \'*/base\' ! -path \'dist\'`;do cp -rf dist/base $i;done && rm -rf dist/base',
                // command: 'for i in `find dist -type d -maxdepth 1 ! -path \'dist\' ! -path \'*/base\' ! -path \'**/${i##*/}\'`;do mkdir $i/${i##*/}; cp -rf $i $i/${i##*/}; cp -rf dist/base $i; done && rm -rf dist/base',
                command: 'for i in `find dist -type d -maxdepth 1 ! -path \'dist\' ! -path \'*/base\'`;do mv $i dist/tmp && mkdir $i && cp -rf dist/tmp $i/${i##*/} && rm -rf dist/tmp && cp -rf dist/base $i; done && rm -rf dist/base',
            },

        },

    });

    // 加载提供任务的插件
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');
    grunt.loadNpmTasks('grunt-contrib-imagemin');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-shell');
    grunt.loadNpmTasks('grunt-contrib-less');

    //grunt single:1.0.3 --site=irm
    grunt.registerTask('single', 'Automated packaging and compression, modify version number', function(arg1) {
        grunt.config('version', arg1);
        grunt.config('site', grunt.option('site'));
        grunt.task.run(['shell:single', 'uglify:base', 'uglify:site', 'less:target', 'cssmin:base', 'cssmin:site', 'imagemin:base', 'imagemin:site', 'copy:base', 'copy:site', 'htmlmin:site']);
        grunt.log.writeln("Package, the current version is modified to v" + arg1);
    });
    //grunt all:1.0.4
    grunt.registerTask('all', 'Automated packaging and compression, modify version number', function(arg1) {
        grunt.config('version', arg1);
        grunt.task.run(['shell:target', 'uglify:target', 'less:target', 'cssmin:target', 'imagemin:target', 'copy:target', 'htmlmin:target', 'shell:copy_base']);
    });

    grunt.registerTask('mv', ['shell:target']);
};